/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.view.gui.listar;

import app.entities.model.DTO.MatriculaAlunoDTO;
import app.view.classes.ViewAluno;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Murilo
 */
public class ListaAlunos extends javax.swing.JFrame {

    private ArrayList<MatriculaAlunoDTO> ListaAlunosMatriculados;
    private ArrayList<MatriculaAlunoDTO> ListaAlunosMatriculadosNaTurma;
    private JTable tabela;
    private ViewAluno viewAluno;

    public ListaAlunos(JTable Tabela, ArrayList<MatriculaAlunoDTO> AlunosMatriculados) {
        initComponents();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        pack();
        setLocation((screenSize.width/2) - (getWidth()/2), (screenSize.height/2) - (getHeight()/2));
        this.viewAluno = new ViewAluno();
        this.ListaAlunosMatriculados = viewAluno.retornarListaAlunosMAtriculados();
        this.ListaAlunosMatriculadosNaTurma = AlunosMatriculados;
        this.tabela = Tabela;
        DefaultTableModel model = (DefaultTableModel) this.tblAlunos.getModel();
        for (MatriculaAlunoDTO MatriculaAluno : this.ListaAlunosMatriculados) {
            boolean estaNaListaDeMatriculados = false;
            for (MatriculaAlunoDTO alunoJaMatriculado : this.ListaAlunosMatriculadosNaTurma) {
                if (MatriculaAluno.getId() == alunoJaMatriculado.getId()) {
                    estaNaListaDeMatriculados = true;
                    break;
                }
            }
            if (!estaNaListaDeMatriculados) {
                model.addRow(new Object[]{MatriculaAluno.getId(), MatriculaAluno.getCodigoMatricula(),
                            MatriculaAluno.getAluno().getNome(), MatriculaAluno.getAluno().getSobrenome()});
            }
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblAlunos = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lista de Alunos Matriculados");

        tblAlunos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Matrícula", "Nome", "Sobrenome"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblAlunos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAlunosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblAlunos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblAlunosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAlunosMouseClicked
        if (tblAlunos.getSelectedRow() != -1) {
            int RegistroSelecionado = this.tblAlunos.getSelectedRow();
            String Id = String.valueOf(((DefaultTableModel) this.tblAlunos.getModel()).getValueAt(RegistroSelecionado, 0));
            String Matricula = String.valueOf(((DefaultTableModel) this.tblAlunos.getModel()).getValueAt(RegistroSelecionado, 1));
            String Nome = String.valueOf(((DefaultTableModel) this.tblAlunos.getModel()).getValueAt(RegistroSelecionado, 2));
            String Sobrenome = String.valueOf(((DefaultTableModel) this.tblAlunos.getModel()).getValueAt(RegistroSelecionado, 3));
            MatriculaAlunoDTO novaMatricula = new MatriculaAlunoDTO();
            for (MatriculaAlunoDTO matriculaAluno : this.ListaAlunosMatriculados) {
                if (Integer.valueOf(Id) == matriculaAluno.getId()) {
                    this.ListaAlunosMatriculadosNaTurma.add(matriculaAluno);
                }
            }
            DefaultTableModel model = (DefaultTableModel) this.tabela.getModel();
            model.addRow(new Object[]{Integer.valueOf(Id), Matricula, Nome, Sobrenome});
            this.dispose();
        }
    }//GEN-LAST:event_tblAlunosMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAlunos;
    // End of variables declaration//GEN-END:variables
}
